export const importFile = async ({ accept } = {}) => {
    return new Promise((resolve, reject) => {
        // let rtfTextContent = '';
        // 创建 input 元素
        const input = document.createElement('input');
        input.type = 'file';
        input.accept = accept || '.rtf';
        let isOpenFile = false;
        // 监听文件选择事件
        input.onchange = async (event) => {
          // @ts-ignore
            const file = event.target.files[0];
            if (file) {
                isOpenFile = true;
                // 读取文件内容
                const reader = new FileReader();
                reader.onload = (e) => {
          // @ts-ignore
                    const ab = e.target.result;
                    // 当文件读取完成后，调用 resolve 函数将结果传递出去
                    resolve({
                      name: file.name,
          // @ts-ignore
                      arrayBuffer: new Uint8Array(ab)
                    });
                };
                reader.onerror = (error) => {
                    // 如果文件读取出现错误，调用 reject 函数
                    reject(error);
                };
                reader.readAsArrayBuffer(file);
            }
        };
        // 取消事件
        window.addEventListener(
            'focus',
            () => {
                setTimeout(() => {
                    if (!isOpenFile) {
                        // 取消逻辑处理
                        reject(new Error('User cancelled file selection'));
                    }
                }, 300);
            },
            { once: true }
        );

        // 触发文件选择对话框
        input.click();
    });
  };


  
  export function arrayBufferToBase64(buffer) {
        var binary = '';
        var bytes = new Uint8Array(buffer);
        var len = bytes.byteLength;
        for (var i = 0; i < len; i++) {
              binary += String.fromCharCode(bytes[i]);
        }
        return window.btoa(binary);
      }
  
      export function generateBase64Prefix(fileName) {
        const fileExtension = fileName.split('.').pop().toLowerCase();
        switch (fileExtension) {
            case 'jpg':
            case 'jpeg':
                return 'data:image/jpeg;base64,';
            case 'png':
                return 'data:image/png;base64,';
            case 'gif':
                return 'data:image/gif;base64,';
            case 'pdf':
                return 'data:application/pdf;base64,';
            case 'txt':
                return 'data:text/plain;base64,';
            case 'csv':
                return 'data:text/csv;base64,';
            case 'xml':
                return 'data:text/xml;base64,';
            case 'json':
                return 'data:application/json;base64,';
            // OnlyOffice 支持的文档格式
            case 'doc':
            case 'docx':
                return 'data:application/msword;base64,';
            case 'odt':
                return 'data:application/vnd.oasis.opendocument.text;base64,';
            case 'rtf':
                return 'data:application/rtf;base64,';
            // OnlyOffice 支持的表格格式
            case 'xls':
            case 'xlsx':
                return 'data:application/vnd.ms-excel;base64,';
            case 'ods':
                return 'data:application/vnd.oasis.opendocument.spreadsheet;base64,';
            // OnlyOffice 支持的演示文稿格式
            case 'ppt':
            case 'pptx':
                return 'data:application/vnd.ms-powerpoint;base64,';
            case 'odp':
                return 'data:application/vnd.oasis.opendocument.presentation;base64,';
            default:
                return 'data:application/octet-stream;base64,';
        }
      }
        